package com.yanqu.road.dao.impl.activity.fivehero;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroUnion;
import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroUnionMember;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFiveHeroUnionDaoImpl extends TempDataObjectDao<CrossFiveHeroUnion> {

    public static String getCrossFiveHeroUnionSql = "select * from `t_c_activity_fivehero_union_data` " +
            "where `activityId`=? AND groupId = ?";

    public Map<String,CrossFiveHeroUnion> getCrossFiveHeroUnion(int activityId,int groupId) {
        Map<String,CrossFiveHeroUnion> resMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFiveHeroUnionSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFiveHeroUnion unionData = parse(rs);
                resMap.put(unionData.getUnionUid(),unionData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resMap;
    }

    public static String updateCrossFiveHeroUnionSql = "update `t_c_activity_fivehero_union_data` set " +
            "unionBaseInfo=?,memberMap=?,campId=?,rank=?,totalAbility=?" +
            " where activityId = ? AND unionUid=? AND groupId=?";

    @Override
    protected boolean updateData(CrossFiveHeroUnion crossFiveHeroUnionData) {
        crossFiveHeroUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroUnionData.getMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFiveHeroUnionData.getTotalAbility()));

        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossFiveHeroUnionData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getGroupId()));

        if (getDb().executeNoneQuery(updateCrossFiveHeroUnionSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossFiveHeroUnionSql = "insert into `t_c_activity_fivehero_union_data`" +
            "(activityId,unionUid,unionBaseInfo,memberMap,campId,groupId,rank,totalAbility)" +
            " values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossFiveHeroUnion crossFiveHeroUnionData) {
        crossFiveHeroUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossFiveHeroUnionData.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroUnionData.getMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroUnionData.getRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFiveHeroUnionData.getTotalAbility()));
        if (getDb().executeNoneQuery(insertCrossFiveHeroUnionSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossFiveHeroUnion parseData(ResultSet rs) throws SQLException {
        CrossFiveHeroUnion crossFiveHeroUnionData = new CrossFiveHeroUnion();
        crossFiveHeroUnionData.setGroupId(rs.getInt("groupId"));
        crossFiveHeroUnionData.setActivityId(rs.getInt("activityId"));
        crossFiveHeroUnionData.setUnionUid(rs.getString("unionUid"));
        crossFiveHeroUnionData.setCampId(rs.getInt("campId"));
        crossFiveHeroUnionData.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), new TypeReference<UnionBaseInfo>() {
        }));
        crossFiveHeroUnionData.setMemberMap(JSON.parseObject(rs.getString("memberMap"), new TypeReference<ConcurrentHashMap<Long, CrossFiveHeroUnionMember>>() {
        }));
        crossFiveHeroUnionData.setRank(rs.getInt("rank"));
        crossFiveHeroUnionData.setTotalAbility(rs.getLong("totalAbility"));
        return crossFiveHeroUnionData;
    }

}
